//
//(C) Copyright 2011 HongKong Airline.,Ltd. 
//
/**
 * systemUserDaoHibernate
 *
 * 项目名称:代购系统
 *
 * @author Mengxiangliang
 *
 * 功能描述:
 * 
 * 历史版本:2013-01-04 v1.0.0 Mengxiangliang  创建:
 * 
 */
package com.gamutsoft.amb.user.dao.impl;

import java.util.List;
import java.util.Map;

import com.gamutsoft.amb.common.bean.QueryExample;
import com.gamutsoft.amb.common.dao.impl.GenericDaoHibernate;
import com.gamutsoft.amb.common.exception.BusinessException;
import com.gamutsoft.amb.user.dao.ISystemUserDao;
import com.gamutsoft.amb.user.model.SystemUser;

@SuppressWarnings("unchecked")
public class SystemUserDaoHibernate extends GenericDaoHibernate<SystemUser, Long> implements ISystemUserDao {
    
     public SystemUserDaoHibernate() {
		super(SystemUser.class);

	}
    
    /**
	 * 根据条件查询记录
	 * @author Mengxiangliang
	 * @param  QueryExample
	 * @return List<Map>
	 * @throws BusinessException
	 */
	@Override
	public List<Map> searchSystemUserList(QueryExample qe) throws BusinessException {
	
	    StringBuffer sb = new StringBuffer();
		sb.append("SELECT T.ID,T.USERNAME,T.EMAIL,T.PASSWORD,T.COUNTRY,T.CITY,T.HOMETOWN,T.TRUENAME,T.SEX,T.BIRTHDAY,T.PHONE,T.HOMELINKPERSON,T.HOMELINKPHONE,T.PERSON_IMG,T.STATUS,T.CREATE_TIME FROM SYSTEM_USER T");
		//qe.addOrderBy("");
		return this.findByQueryExample(sb.toString(), qe);
		
	}

	/**
	 * 根据ID查询记录
	 * @author Mengxiangliang
	 * @param id
	 * @return
	 * @throws BusinessException
	 */
	@Override
	public SystemUser searchSystemUserById(Long id) throws BusinessException {
	
	  SystemUser domain = new SystemUser();
	   String hql = "from SystemUser where id=?";
	   List<SystemUser> list = this.getHibernateTemplate().find(hql, id);
	   if(list != null && list.size() >0){
		  domain = list.get(0);
	   }
	   return domain;
		
	}

	/**
	 * 删除记录
	 * @author Mengxiangliang
	 * @param id
	 * @throws BusinessException
	 */
	@Override
	public void deleteSystemUserById(Long id) throws BusinessException {
	
	 String hql ="delete from SystemUser where id=?";
	 this.getHibernateTemplate().bulkUpdate(hql, id);
	 
	}

	@Override
	public List<Map> searchSystemUserListByWhere(QueryExample qe,
			SystemUser domain) throws BusinessException {
		StringBuffer sb = new StringBuffer();
		sb.append("SELECT T.ID,T.USERNAME,T.EMAIL,T.PASSWORD,T.COUNTRY,T.CITY,T.HOMETOWN,T.TRUENAME,T.SEX,T.BIRTHDAY,T.PHONE,T.HOMELINKPERSON,T.HOMELINKPHONE,T.PERSON_IMG,T.STATUS,T.CREATE_TIME FROM SYSTEM_USER T");
		if(domain!=null){
			if(domain.getEmail()!=null && !"".equals(domain.getEmail())){
				qe.addCondictions("T.EMAIL","=",domain.getEmail());
			}
			if(domain.getUsername()!=null && !"".equals(domain.getUsername())){
				qe.addCondictions("T.USERNAME","=",domain.getUsername());
			}
			if(domain.getPassword()!=null && !"".equals(domain.getPassword())){
				qe.addCondictions("T.PASSWORD","=",domain.getPassword());
			}
		}
		qe.addOrderBy(" T.CREATE_TIME desc ");
		return this.findByQueryExample(sb.toString(), qe);
	}

	@Override
	public SystemUser loginByUser(SystemUser bean) throws BusinessException {
		  SystemUser domain = null;
		  String hql="";
		  if(bean!=null){
			  if(bean.getUsername()!=null &&!"".equals(bean.getUsername())){
				  hql = "from SystemUser where username=? and password=?";
				  List<SystemUser> list = this.getHibernateTemplate().find(hql, bean.getUsername(),bean.getPassword());
				   if(list != null && list.size() >0){
					  domain = list.get(0);
				   }
			  }else if(bean.getEmail()!=null && !"".equals(bean.getEmail())){
				  hql = "from SystemUser where email=? and password=?";
				  List<SystemUser> list = this.getHibernateTemplate().find(hql, bean.getEmail(),bean.getPassword());
				   if(list != null && list.size() >0){
					  domain = list.get(0);
				   }
			  }
		  }
		   
		   return domain;
	}
	
}